﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

public partial class DistributeRole : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            db();
        }
    }
    void db()
    {
        CMSModel.CMSEntities cms=  DBManager.GetCmsEmtity();
        int roleId = int.Parse(Request["roleId"]);
        var role = cms.Role.First(x => x.RoleId == roleId);
        lblRoleName.Text = role.RoleName;
        table.DataSource = cms.Power.Where(x => x.Pid == "0");
        table.DataBind();
    }


    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        var c = (role_Role)e.Item.FindControl("RoleRight1");
        int roleId = int.Parse(Request["roleId"]);
        c.SetValue(roleId);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            int roleId = int.Parse(Request["roleId"]);
            CMSModel.CMSEntities cms = DBManager.GetCmsEmtity();
            var delObj = cms.Role_Power.Where(x => x.RoleId == roleId);
            foreach (var tt in delObj)
            {
                cms.DeleteObject(tt);
            }
            for (int i = 0; i < table.Items.Count; i++)
            {
                DataListItem item = table.Items[i];
                var c = (role_Role)item.FindControl("RoleRight1");
                string[] values = c.GetValue();
                cms.SaveChanges();
                foreach (var tt in values)
                {
                    cms.Role_Power.AddObject(new CMSModel.Role_Power() { RoleId = roleId, Pow_Id = tt });
                }
                cms.SaveChanges();

            }
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('更新成功');document.location='Default.aspx'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "editDistributeRole", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('更新失败!')", true);
        }

    }
}